package tuding.android.bigplanettracks.maps.tuding;


import tuding.android.bigplanettracks.maps.RawTile;
import tuding.android.bigplanettracks.maps.storage.SQLLocalStorage;


import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Point;
import android.util.Log;

public class GMOT {

	public static final String DATABASE_NAME = "gmot.db";
	public static final String TABLE_NAME = "gmot";

	private static final String COLUMN_TX = "tilex";

	private static final String COLUMN_TY = "tileY";

	private static final String COLUMN_00X = "f00x";
	private static final String COLUMN_00Y = "f00y";
	private static final String COLUMN_01X = "f01x";
	private static final String COLUMN_01Y = "f01y";
	private static final String COLUMN_02X = "f02x";
	private static final String COLUMN_02Y = "f02y";
	private static final String COLUMN_03X = "f03x";
	private static final String COLUMN_03Y = "f03y";
	private static final String COLUMN_10X = "f10x";
	private static final String COLUMN_10Y = "f10y";
	private static final String COLUMN_11X = "f11x";
	private static final String COLUMN_11Y = "f11y";
	private static final String COLUMN_12X = "f12x";
	private static final String COLUMN_12Y = "f12y";
	private static final String COLUMN_13X = "f13x";
	private static final String COLUMN_13Y = "f13y";
	private static final String COLUMN_20X = "f20x";
	private static final String COLUMN_20Y = "f20y";
	private static final String COLUMN_21X = "f21x";
	private static final String COLUMN_21Y = "f21y";
	private static final String COLUMN_22X = "f22x";
	private static final String COLUMN_22Y = "f22y";
	private static final String COLUMN_23X = "f23x";
	private static final String COLUMN_23Y = "f23y";
	private static final String COLUMN_30X = "f30x";
	private static final String COLUMN_30Y = "f30y";
	private static final String COLUMN_31X = "f31x";
	private static final String COLUMN_31Y = "f31y";
	private static final String COLUMN_32X = "f32x";
	private static final String COLUMN_32Y = "f32y";
	private static final String COLUMN_33X = "f33x";
	private static final String COLUMN_33Y = "f33y";



	public static final String TABLE_DDL = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
			+ "(" + 
			COLUMN_TX + " integer DEFAULT '0'," + 
			COLUMN_TY + " integer DEFAULT '0'," +
			COLUMN_00X + " integer DEFAULT '0'," +
			COLUMN_00Y + " integer DEFAULT '0'," +
			COLUMN_01X + " integer DEFAULT '0'," +
			COLUMN_01Y + " integer DEFAULT '0'," +
			COLUMN_02X + " integer DEFAULT '0'," +
			COLUMN_02Y + " integer DEFAULT '0'," +
			COLUMN_03X + " integer DEFAULT '0'," +
			COLUMN_03Y + " integer DEFAULT '0'," +	
			COLUMN_10X + " integer DEFAULT '0'," +
			COLUMN_10Y + " integer DEFAULT '0'," +
			COLUMN_11X + " integer DEFAULT '0'," +
			COLUMN_11Y + " integer DEFAULT '0'," +
			COLUMN_12X + " integer DEFAULT '0'," +
			COLUMN_12Y + " integer DEFAULT '0'," +
			COLUMN_13X + " integer DEFAULT '0'," +
			COLUMN_13Y + " integer DEFAULT '0'," +
			COLUMN_20X + " integer DEFAULT '0'," +
			COLUMN_20Y + " integer DEFAULT '0'," +
			COLUMN_21X + " integer DEFAULT '0'," +
			COLUMN_21Y + " integer DEFAULT '0'," +
			COLUMN_22X + " integer DEFAULT '0'," +
			COLUMN_22Y + " integer DEFAULT '0'," +
			COLUMN_23X + " integer DEFAULT '0'," +
			COLUMN_23Y + " integer DEFAULT '0'," +
			COLUMN_30X + " integer DEFAULT '0'," +
			COLUMN_30Y + " integer DEFAULT '0'," +
			COLUMN_31X + " integer DEFAULT '0'," +
			COLUMN_31Y + " integer DEFAULT '0'," +
			COLUMN_32X + " integer DEFAULT '0'," +
			COLUMN_32Y + " integer DEFAULT '0'," +
			COLUMN_33X + " integer DEFAULT '0'," +
			COLUMN_33Y + " integer DEFAULT '0'" +
			");";

	private static SQLiteDatabase db;

	
	public GMOT() {
		if (db == null) {
			String sqliteFilePath = SQLLocalStorage.OFFSET_PATH + DATABASE_NAME;
			db = SQLiteDatabase.openDatabase(sqliteFilePath, null,
					SQLiteDatabase.OPEN_READONLY);
			db.execSQL(TABLE_DDL);
		}
	}
	
	
	 
	
	
	// offset data is for zoom = 6
	// only not find offset value in GMOffsetList will come to this routine
	public Point getOffsetByTileNum(RawTile tile) {
		//if(tile.s == 0) { // for google map
		if(true) { // sourceID will be ensure in higher level
			int vindex_x = tile.x;
			int vindex_y = tile.y;
			if(7>=tile.z) {
				vindex_x = tile.x >> (7-tile.z);
				vindex_y = tile.y >> (7-tile.z);
			} else {
				vindex_x = tile.x << (tile.z-7);
				vindex_y = tile.y << (tile.z-7);
			}
			//Log.i("LL", "getOffsetByTileNum index: "+vindex_x+" "+vindex_y);
			int rindex_x = vindex_x >> 2;
			int rindex_y = vindex_y >> 2;
			int vindex_x_q = rindex_x << 2;
			int vindex_y_q = rindex_y << 2;
			int rindex_x_rem = vindex_x%4;
			int rindex_y_rem = vindex_y%4;
			Log.i("LL", "real index: "+rindex_x+" "+rindex_y+" "+rindex_x_rem+" "+rindex_y_rem);
			if(qulifyXY(vindex_x, vindex_y)) {
				try { 
					Point offset = new Point(0, 0);
					Cursor c = db.rawQuery("select * from "+TABLE_NAME+" where "+COLUMN_TX+"="+ rindex_x+" and "+COLUMN_TY+"="+rindex_y, null);
					//Cursor c = db.query(true, TABLE_NAME, null, COLUMN_TX+"="+ rindex_x+" and "+COLUMN_TY+"="+rindex_y, null,
					//		null, null, null, null);
					//Log.i("OFFSET","query: "+"select * from "+TABLE_NAME+" where "+COLUMN_TX+"="+ rindex_x+" and "+COLUMN_TY+"="+rindex_y);
					// init to avoid replicated access db
					GMOffsetList.GMOffsets.clear();
					for(int i=0; i<4; i++) {
						for(int j=0; j<4; j++) {
							//Log.i("OFFSET", "Prepare GMOffsetList data : "+(vindex_x_q+i)+" "+(vindex_y_q+j));
							GMOffsetBase tmpGMOB = new GMOffsetBase(
									vindex_x_q+i,
									vindex_y_q+j,
									0,
									0
									);
							GMOffsetList.GMOffsets.add(tmpGMOB);
						}
					}
					if(c != null && c.getCount() == 1) {
						// prepare a list cache for later 
						// this cache has 16 entries
						c.moveToFirst();
						GMOffsetList.GMOffsets.clear();
						for(int i=0; i<4; i++) {
							for(int j=0; j<4; j++) {
								//Log.i("OFFSET", "Preparing GMOffsetList data : "+(vindex_x_q+i)+" "+(vindex_y_q+j));
								GMOffsetBase tmpGMOB = new GMOffsetBase(
										vindex_x_q+i,
										vindex_y_q+j,
										c.getInt(i*8+j*2+2),
										c.getInt(i*8+j*2+3)
										);
								//Log.w("OFFSET", "list size: "+GMOffsetList.GMOffsets.size());
								//Log.i("OFFSET", "pump into offset data:"+c.getInt(i*8+j*2+2)+" "+c.getInt(i*8+j*2+3));
								GMOffsetList.GMOffsets.add(tmpGMOB);
							}
						}
						offset.x = c.getInt(rindex_x_rem*8+rindex_y_rem*2+2);
						offset.y = c.getInt(rindex_x_rem*8+rindex_y_rem*2+3);
						Log.i("LL", "--->offset: "+offset.x+" "+offset.y);
					} else {
						//Log.i("OFFSET", "--->c count is "+c.getCount());
						Log.e("OFFSET", "c is null or count is more than 1: "+c.getCount());
					}
					c.close();
					return offset;
				} catch (SQLException e) {
					e.printStackTrace();
					Log.e("OFFSET", e.toString());
				}
			}
		}
		Log.i("OFFSET", "--->offset is null!");
		return new Point(0, 0);
	}
	
	
	
	private boolean qulifyXY(int x, int y) {
		if(x>724 && x<896 && y>332 && y<460) return true;
		else return false;
	}




	
	public void close() {
		if (db != null) {
			db.close();
			db = null;
		}
	}
}
